/*
 *--------------------------------------------------------------------------
 *   Battlefield - A Realtime Network Multiplayer Game
 *   =======================================================
 *   Developed by Group D02 - 2009/2010 Semester 4 - CS2103
 *     Harry Nguyen Duy Hoang <nnduyhoang@yahoo.co.uk>
 *     Kent Chng Siang Rong <fivefootway@gmail.com>
 *     Lim Yong Peng <limpeng1986@gmail.com>
 *     Loh Xiankun <u0807185@nus.edu.sg>
 *   Instructed by
 *     Dr. Damith C.Rajapakse <damith@gmail.com>
 *   =======================================================
 *   $Id: MessagePanel.java 604 2010-07-26 19:19:28Z Harry $
 *   $LastChangedDate: 2010-07-26 19:19:28 +0000 (Mon, 26 Jul 2010) $
 *   $LastChangedBy: Harry $
 *--------------------------------------------------------------------------
 */
package battlefield.ui.component;

import battlefield.resource.Graphics;
import java.awt.Color;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Kent
 */
public class MessagePanel extends JPanel implements ActionListener {

    ImageIcon backImage;
    Image image;
    JLabel msgLab;
    Timer t;
    int count;

    public MessagePanel() {
        backImage = new ImageIcon(Graphics.PANEL_MESSAGE.getImage());
        image = backImage.getImage();
        t = new Timer(500, this);
        setOpaque(false);
        initComponents();
    }

    private void initComponents() {
        msgLab = new JLabel();
        msgLab.setForeground(Color.WHITE);
        this.add(msgLab);
    }

    public void updateMessage(String msg) {
        msgLab.setText(msg);
        count = 0;
        t.start();
    }

    @Override
    public void paintComponent(java.awt.Graphics g) {
        g.drawImage(image, 0, 0, this);
        super.paintComponent(g);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(t)) {
            if ((count % 2) == 0) {
                msgLab.setForeground(Color.RED);
            } else {
                msgLab.setForeground(Color.WHITE);
            }
            if (count == 5) {
                t.stop();
                msgLab.setText("");
            }
            count++;
        }
    }

    @Override
    protected void finalize() throws Throwable {
        if (t != null && t.isRunning()) {
            t.stop();
        }
        t = null;
        super.finalize();
    }
}
